# author = '李天宝'
# write in '2020/8/4 0004 14:54'

from rest_framework.views import APIView
from rest_framework.response import Response
from LoginSign.models import User
from common.moexception import NoDataException

class Login(APIView):
    """
    用户的注册逻辑，
    username:用户名 必须
    telephone:电话 必须
    email:邮箱
    introduction：简介
    password:密码 必须
    """

    authentication_classes = []
    permission_classes = []

    def post(self,request):

        data=request.data

        if 'username' not in data or 'password' not in data:
            NoDataException("没有username，或者没有password字段")

        user=User.objects.create_user(username=data['username'],password=data['password'])

        if 'introduction' in data:
            user.introduction=data['introduction']

        if 'email' in data:
            user.email=data['email']

        if 'telephone' not in data:
            # raise NoDataException("没有telephone选项")
            user.telephone=data['telephone']

        user.save()

        return Response({
            'code':200,
            'detail':'注册成功'
        })